package leetcode;

import java.util.Arrays;

public class LC16 {
    public static void main(String[] args) {
        LC16 lc16 = new LC16();
        System.out.println(Arrays.toString(lc16.productExceptSelf(new int[]{-1,1,0,-3,3})));
        System.out.println(Arrays.toString(lc16.productExceptSelf(new int[]{1,2,3,4})));
        System.out.println(Arrays.toString(lc16.productExceptSelf(new int[]{1,2,3,4,5})));
    }
    // 1   2  3  4  5
    // 1   1  2  6  24
    // 120 60 20 5  1
    // 120 60 40 30 24
    public int[] productExceptSelf(int[] nums) {
        int[] answer = new int[nums.length];
        Arrays.fill(answer,1);
        int left = 0, right = nums.length - 1;
        int lp = 1, rp = 1;
        while (right >= 0 && left < nums.length) {
            answer[right] *= rp;
            answer[left] *= lp;
            System.out.println(rp);
            System.out.println(lp);
            System.out.println(Arrays.toString(answer));
            lp *= nums[left++];
            rp *= nums[right--];
        }
        return answer;
    }
}
